﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Pattern.Common
{
    public static class Serialization
    {
        public static void Serialize<T>(string path, T instance)
        {
            var serializer = new XmlSerializer(typeof(T));
            var xmlDir = new FileInfo(path).Directory;
            if (xmlDir != null && !xmlDir.Exists) xmlDir.Create();
            using (var writer = new StreamWriter(path))
            {
                serializer.Serialize(writer, instance);
            }
        }
        public static T Deserialize<T>(string path)
        {
            var serializator = new XmlSerializer(typeof(T));
            using (var reader = new StreamReader(path))
            {
                return (T)serializator.Deserialize(reader);
            }
        }
    }
}
